﻿using System.Diagnostics;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Xml.Serialization;
using WinFormsApp1;

namespace AHC
{
    public class Utils
    {
        public static string ObjToSHA256(AhcObject obj)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AhcObject));
            StringWriter sw = new StringWriter();
            serializer.Serialize(sw, obj);
            string xml = sw.ToString();
            SHA256 sha256 = SHA256.Create();
            byte[] hashValue = sha256.ComputeHash(Encoding.Default.GetBytes(xml));
            string result = BitConverter.ToString(hashValue, 4, 8);
            return result;
        }

        public static bool SaveToXMLFile(string filename, AhcObject obj)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AhcObject));
            TextWriter writer = new StreamWriter(filename);
            
            Trace.WriteLine(writer.ToString());

            serializer.Serialize(writer, obj);
            writer.Close();

            return true;
        }

        public static AhcObject ReadFromXMLFile(string filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AhcObject));

            FileStream fs = new FileStream(filename, FileMode.Open);
            AhcObject obj;
            obj = (AhcObject)serializer.Deserialize(fs);

            fs.Close();

            return obj;
        }

        /**
         * 将字符串在固定长度内居中（前面加空格）
         * @param text 需要被居中的字符串
         * @param length 在该长度中居中
         */
        public static string CenterString(string text, int length)
        {
            if (text == null || text.Length == 0)
            {
                return "";
            }
            // 若相差零个/一个字符（如16位输入16/15位则不处理）
            else if (length - text.Length > 1) { 
                int fillLength = (int)(length - text.Length) / 2;
                for (int i = 0; i < fillLength; i++)
                {
                    text = " " + text;
                }
            }
            
            return text; 
        }

        public static void WriteConfig(string path)
        {
            string filename = Application.StartupPath.ToString() + "\\config.txt";
            StreamWriter sw = new StreamWriter(filename, false);
            sw.WriteLine(path);
            sw.Close();
        }

        public static string ReadConfig()
        {
            string filename = Application.StartupPath.ToString() + "\\config.txt";

            if (!File.Exists(filename)) return "";

            StreamReader sr = new StreamReader(filename);
            string path = sr.ReadLine();
            sr.Close();
            return path;
        }


        public static bool switchTerminal(Terminal t1, Terminal t2)
        {
            Terminal tmp = t1.Clone();
            t1.terminalAnnaxSide = t2.terminalAnnaxSide;
            Type type =typeof(Terminal);
            

            return true;
        }
    }
}
